home *** CD-ROM | disk | FTP | other *** search
- import java.applet.Applet;
- import java.awt.Canvas;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Event;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.Rectangle;
- import java.awt.image.FilteredImageSource;
- import java.awt.image.ImageFilter;
- import java.awt.image.ImageObserver;
- import java.awt.image.ImageProducer;
-
- class ImageCanvas extends Canvas implements ImageObserver {
- double hmult;
- int xadd;
- int yadd;
- int imgw = -1;
- int imgh = -1;
- int scalew = -1;
- int scaleh = -1;
- boolean focus = false;
- boolean usefilter = false;
- static final int numalphas = 8;
- int alpha = 7;
- Image[] imagevariants = new Image[16];
- ImageFilter colorfilter;
- Image origimage;
- Image curimage;
- Applet applet;
- static final long updateRate = 100L;
-
- public ImageCanvas(Applet app, Image img, double mult) {
- this.applet = app;
- this.origimage = img;
- this.imagevariants[7] = this.origimage;
- this.hmult = mult;
- this.pickImage();
- ((Component)this).reshape(0, 0, 100, 100);
- }
-
- public void gotFocus() {
- this.focus = true;
- ((Component)this).repaint();
- }
-
- public void lostFocus() {
- this.focus = false;
- ((Component)this).repaint();
- }
-
- public void paint(Graphics g) {
- Rectangle r = ((Component)this).bounds();
- if (this.focus) {
- g.setColor(Color.red);
- } else {
- g.setColor(Color.darkGray);
- }
-
- g.drawRect(0, 0, r.width - 1, r.height - 1);
- g.drawLine(0, 0, r.width, r.height);
- g.drawLine(r.width, 0, 0, r.height);
- g.drawLine(0, r.height / 2, r.width, r.height / 2);
- g.drawLine(r.width / 2, 0, r.width / 2, r.height);
- if (this.imgw < 0) {
- this.imgw = this.curimage.getWidth(this);
- this.imgh = this.curimage.getHeight(this);
- if (this.imgw < 0 || this.imgh < 0) {
- return;
- }
- }
-
- if (this.scalew < 0) {
- this.scalew = (int)((double)this.imgw * this.hmult);
- this.scaleh = (int)((double)this.imgh * this.hmult);
- }
-
- if (this.imgw == this.scalew && this.imgh == this.scaleh) {
- g.drawImage(this.curimage, this.xadd, this.yadd, this);
- } else {
- g.drawImage(this.curimage, this.xadd, this.yadd, this.scalew, this.scaleh, this);
- }
- }
-
- public synchronized boolean imageUpdate(Image img, int infoflags, int x, int y, int w, int h) {
- if (img != this.curimage) {
- return false;
- } else {
- boolean ret = true;
- boolean dopaint = false;
- long updatetime = 0L;
- if ((infoflags & 1) != 0) {
- this.imgw = w;
- dopaint = true;
- }
-
- if ((infoflags & 2) != 0) {
- this.imgh = h;
- dopaint = true;
- }
-
- if ((infoflags & 48) != 0) {
- dopaint = true;
- ret = false;
- } else if ((infoflags & 8) != 0) {
- dopaint = true;
- updatetime = 100L;
- }
-
- if ((infoflags & 64) != 0) {
- ret = false;
- }
-
- if (dopaint) {
- ((Component)this).repaint(updatetime);
- }
-
- return ret;
- }
- }
-
- public synchronized Image pickImage() {
- int index = this.alpha;
- if (this.usefilter) {
- index += 8;
- }
-
- Image choice = this.imagevariants[index];
- if (choice == null) {
- choice = this.imagevariants[this.alpha];
- if (choice == null) {
- int alphaval = this.alpha * 255 / 7;
- ImageFilter imgf = new AlphaFilter(alphaval);
- ImageProducer src = this.origimage.getSource();
- ImageProducer var8 = new FilteredImageSource(src, imgf);
- choice = this.applet.createImage(var8);
- this.imagevariants[this.alpha] = choice;
- }
-
- if (this.usefilter) {
- if (this.colorfilter == null) {
- this.colorfilter = new RedBlueSwapFilter();
- }
-
- ImageProducer src = choice.getSource();
- ImageProducer var7 = new FilteredImageSource(src, this.colorfilter);
- choice = this.applet.createImage(var7);
- }
-
- this.imagevariants[index] = choice;
- }
-
- this.curimage = choice;
- return choice;
- }
-
- public synchronized boolean handleEvent(Event e) {
- switch (e.id) {
- case 401:
- case 403:
- switch (e.key) {
- case 114:
- case 7:
- this.xadd += 5;
- ((Component)this).repaint();
- return true;
- case 6:
- this.xadd -= 5;
- ((Component)this).repaint();
- return true;
- case 5:
- this.yadd += 5;
- ((Component)this).repaint();
- return true;
- case 4:
- this.yadd -= 5;
- ((Component)this).repaint();
- return true;
- case 3:
- if ((e.modifiers & 1) != 0) {
- if (--this.alpha < 0) {
- this.alpha = 0;
- }
-
- this.pickImage();
- } else {
- this.hmult /= 1.2;
- }
-
- this.scalew = this.scaleh = -1;
- ((Component)this).repaint();
- return true;
- case 2:
- if ((e.modifiers & 1) != 0) {
- if (++this.alpha > 7) {
- this.alpha = 7;
- }
-
- this.pickImage();
- } else {
- this.hmult *= 1.2;
- }
-
- this.scalew = this.scaleh = -1;
- ((Component)this).repaint();
- return true;
- case 0:
- this.usefilter = !this.usefilter;
- this.pickImage();
- ((Component)this).repaint();
- return true;
- default:
- return false;
- }
- case 402:
- default:
- return false;
- }
- }
- }
-